/*
 * 创建日期 2009-11-26
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.db.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import  org.fantasy.common.util.NumberUtil;
import  org.fantasy.common.util.StringUtil;

/**
 * 一行数据
 * @author: 王文成
 * @version: 1.0
 * @since 2009-11-26
 */
public class Row implements Map ,Cloneable{

    protected LinkedHashMap<Object, Object> rowMap = new LinkedHashMap<Object, Object>();
    
    public Row(){
    }

    
    public Cell getCell(String name) {
        Cell cell = (Cell) rowMap.get(name);
        if (cell == null) {
            cell = new Cell();
            cell.setName(name);
        }
        return cell;
    }

    @SuppressWarnings("all")
    public List<Cell> cells() {
        return (List<Cell>) new ArrayList(rowMap.values());
    }

    public void clear() {
        rowMap.clear();
    }

    public boolean containsKey(Object key) {
        return rowMap.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return rowMap.containsValue(value);
    }

    public Set entrySet() {
        return rowMap.entrySet();
    }

    public Object get(Object key) {
        return rowMap.get(key);
    }

    public boolean isEmpty() {
        return rowMap.isEmpty();
    }

    public Set keySet() {
        return rowMap.keySet();
    }

    public Object put(Object key, Object value) {
        return rowMap.put(key, value);
    }

    @SuppressWarnings("all")
    public void putAll(Map t) {
        rowMap.putAll(t);
    }

    public Object remove(Object key) {
        return rowMap.remove(key);
    }

    public int size() {
        return rowMap.size();
    }

    public Collection values() {
        return rowMap.values();
    }

    public Date getDate(String key) {
        Cell cell = getCell(key);
        Class cls = cell.getColClass();
        if (cls != null && Date.class.isAssignableFrom(cls)) {
            return (Date) getValue(key);
        } else {
            return null;
        }
    }

    public double getDouble(String key) {
        return getNumber(key).doubleValue();
    }

    public int getInt(String key) {
        return getNumber(key).intValue();
    }
    
    public boolean getBoolean(String key) {
    	String value = getString(key);
        return value.equalsIgnoreCase("true") || value.equals("1");
    }

    public long getLong(String key) {
        return getNumber(key).longValue();
    }

    public Number getNumber(String key) {
        Class cls = getCell(key).getColClass();
        Object value = get(key);
        if (cls != null) {
            if (Number.class.isAssignableFrom(cls)) {
                return (Number) value;
            }
        }
        if (StringUtil.isValid(value)) {
            String v = getString(key);
            boolean isNumber = NumberUtil.isNumber(v);
            if (isNumber)
                return new Double(v);
        }
        return new Double(0);
    }

    public String getString(String key) {
        return getCell(key).getString();
    }

    public Object getValue(String key) {
        return rowMap.get(key);
    }

    public boolean isValid(String key) {
        return StringUtil.isValid(getCell(key));
    }
    
    @Override
    public String toString() {
        return rowMap.toString();
    }

	@Override
	@SuppressWarnings("unchecked")
	public Row clone(){
		Row row = new Row();
		row.rowMap = (LinkedHashMap<Object,Object>)rowMap.clone();
		return row;
	}
}
